package com.shigu.mo.actions;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shigu.linkage.sdks.responses.SubMerchantRegistResponse;
import com.shigu.linkage.sdks.responses.TradeCallbackResponse;
import com.shigu.mo.bo.LinkageMerchantBO;
import com.shigu.mo.bo.requests.LinkageMsgRequest;
import com.shigu.mo.bo.requests.LinkagePayInfoRequest;
import com.shigu.mo.bo.requests.LinkageSubmitRequest;
import com.shigu.mo.exceptions.MoException;
import com.shigu.mo.services.BusinessLinkageService;
import com.shigu.mo.vo.responses.LinkageMsgResponse;
import com.shigu.mo.vo.responses.LinkagePayInfoResponse;
import com.shigu.mo.vo.responses.LinkageSubmitResponse;
import com.shigu.passageway.exceptions.PassagewayException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 供联动专用入口
 */
@Controller
@RequestMapping("/bus/")
public class BusinessLinkageController {

    private static final Logger logger = LoggerFactory.getLogger(BusinessLinkageController.class);

    @Autowired
    BusinessLinkageService businessLinkageService;
    /**
     * 查询卡信息
     * @return
     */
    @RequestMapping("linkagePayInfo")
    @ResponseBody
    public LinkagePayInfoResponse linkagePayInfo(@Valid LinkagePayInfoRequest bo, BindingResult result) throws MoException {
        if (result.hasErrors()) {
            throw new MoException(result.getFieldError().getDefaultMessage());
        }
        return businessLinkageService.selPayInfo(Long.valueOf(bo.getId()));
    }

    /**
     * 申请发送手机号
     * @param bo
     * @param result
     * @return
     * @throws MoException
     */
    @RequestMapping("linkageMsg")
    @ResponseBody
    public LinkageMsgResponse linkageMsg(@Valid LinkageMsgRequest bo,BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new MoException(result.getFieldError().getDefaultMessage());
        }
        businessLinkageService.sendPayMsg(bo);
        return new LinkageMsgResponse().success();
    }

    /**
     * 提交确认交易信息
     * @return
     */
    @RequestMapping("submitLinkagePay")
    @ResponseBody
    public LinkageSubmitResponse submitLinkagePay(@Valid LinkageSubmitRequest bo,BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new MoException(result.getFieldError().getDefaultMessage());
        }
        businessLinkageService.paySure(bo);
        Thread.sleep(3000L);
        if (businessLinkageService.checkOrder(Long.valueOf(bo.getOid()))) {
            return new LinkageSubmitResponse().success();
        }
        Thread.sleep(3000L);
        if (businessLinkageService.checkOrder(Long.valueOf(bo.getOid()))) {
            return new LinkageSubmitResponse().success();
        }
        Thread.sleep(3000L);
        if (businessLinkageService.checkOrder(Long.valueOf(bo.getOid()))) {
            return new LinkageSubmitResponse().success();
        }
        Thread.sleep(3000L);
        if (businessLinkageService.checkOrder(Long.valueOf(bo.getOid()))) {
            return new LinkageSubmitResponse().success();
        }
        return new LinkageSubmitResponse().error("交易正在进行中，请耐心等待，如长时间没到账，请联系工作人员");
    }

    /**
     * 商户进件回调
     * @return
     */
    @RequestMapping("merchantNotify")
    @ResponseBody
    public String merchantNotify(LinkageMerchantBO bo){
        JSONObject obj= JSONObject.parseObject(JSON.toJSONString(bo));
        logger.warn(obj.toJSONString());
        String returnValue=obj.getString("return_value");
        if (StringUtils.isNotEmpty(returnValue)&&returnValue.startsWith("{")) {
            obj.put("return_value",JSONObject.parseObject(returnValue));
        }
        SubMerchantRegistResponse response=JSONObject.toJavaObject(obj,SubMerchantRegistResponse.class);
        businessLinkageService.registCallback(response);
        return "success";
    }

    /**
     * 交易成功回调
     * @param bo
     * @return
     */
    @RequestMapping("linkageTradecall")
    @ResponseBody
    public String linkageTradecall(TradeCallbackResponse bo) throws PassagewayException {
        logger.warn(bo.resultBody());
        businessLinkageService.payCallback(bo);
        return "success";
    }
}
